package com.cictec.middleware.tde.rm.service;

import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.Result;
import com.cictec.middleware.commons.utils.ResultUtil;
import com.cictec.middleware.tde.SendTerminalMessageService;
import com.cictec.middleware.tde.rm.SessionManager;
import com.cictec.middleware.tde.rm.disp.MessageBuilder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x81PlatformMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0xA2PlatformMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0xA6PlatformMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0xBCPlatformMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0xC1PlatformMessage;

/**
 * 【锐明协议】中间件客户端下发消息实现Service
 * 
 * @author Administrator
 *
 */

@Service
public class RmSendTerminalMessageService extends MessageBuilder implements SendTerminalMessageService {

	@Autowired
	private SessionManager sessionManager;

	/**
	 * 判定设备是否在线，并返回返送直接结果
	 * @param msg
	 * @return
	 */
	private Result send(TerminalMessage msg) {
		IoSession session = sessionManager.getSessionByTerminalId(PMSUtils.isNull(msg.getHeader().getTerminalId()));
		if (session != null) {
			session.write(msg);
			return ResultUtil.getSuccessResult("指令发送成功");
		}
		return ResultUtil.getErrorResult("设备不在线，无法发送指令");
	}

	/**
	 * 【0x81】【下发短信，不播放语音提示】【不播放语音0，显示次数2次】
	 */
	@Override
	public Result sendTextMessage(String devCode, String lineId, String content) {
		
//		Header header = new Header(0x81);
//		header.setTerminalId(Integer.parseInt("54867"));
//		Rm0x81PlatformMessage message = new Rm0x81PlatformMessage(header);
//		message.setContent("测试消息");
//		message.setCreateTime(new Date());
//		message.setMessageType(Rm0x81PlatformMessage.MESSAGE_TYPE_TIP);
//		message.setResponseType(0);
//		message.setDisplayOn(Rm0x81PlatformMessage.DISPLAY_ON_DRIVER_LCD);
//		message.setDisplayTimes(2);
//		message.setVoiceType(0);
		
		Rm0x81PlatformMessage msg = createSendMessage(Integer.parseInt(lineId), Integer.parseInt(devCode), Rm0x81PlatformMessage.MESSAGE_TYPE_TIP, 0, Rm0x81PlatformMessage.DISPLAY_ON_DRIVER_LCD, 2, 0, content);
		return send(msg);
	}

	/**
	 * 【0x81】【下发短信，播放语音提示】【播放语音，显示次数2次】
	 */
	@Override
	public Result sendTextVoiceMessage(String devCode, String lineId, String content) {
		Rm0x81PlatformMessage msg = createSendMessage(Integer.parseInt(lineId), Integer.parseInt(devCode), Rm0x81PlatformMessage.MESSAGE_TYPE_TIP, 0, Rm0x81PlatformMessage.DISPLAY_ON_DRIVER_LCD, 2, 1, content);
		return send(msg);
		// return null;
	}

	/**
	 * 【0x81】【下发发车时刻提示】
	 */
	@Override
	public Result sendDepartureTimeMessage(String devCode, String lineId, String content) {
		String _content = "下一趟发车时刻:" + content;
		Rm0x81PlatformMessage msg = createSendMessage(Integer.parseInt(lineId), Integer.parseInt(devCode), Rm0x81PlatformMessage.MESSAGE_TYPE_SMS, 0, Rm0x81PlatformMessage.DISPLAY_ON_DRIVER_LCD, 2, 1, _content);
		return send(msg);
	}
	/**
	 * 【0x81】【前后车距 信息】【播放语音，显示次数2次】
	 */
	@Override
	public Result sendBeforAfterDistanceMessage(String devCode, String lineId, String content) {
		
//		Header header = new Header(0x81);
//		header.setLineId(Integer.parseInt(busInstance.getStatus(Constants.DEVICE_LINE_ID)));
//		header.setTerminalId(Long.parseLong(busInstance.getStatus(Constants.HED_DEV_CODE)));
//		header.setDataFlag(0);
//
//		PlatformDownloadMessage pdm = new PlatformDownloadMessage(header);
//		pdm.setCreateTime(new Date());
//		pdm.setMessageType(0XA0);
//		pdm.setResponseType(0);
//		pdm.setDisplayOn(PlatformDownloadMessage.DISPLAY_ON_DRIVER_LCD);
//		pdm.setDisplayTimes(2);
//		pdm.setVoiceType(0);
//		pdm.setContent(content);
		
		Rm0x81PlatformMessage msg = createSendMessage(Integer.parseInt(lineId), Integer.parseInt(devCode), 0xA0, 0, Rm0x81PlatformMessage.DISPLAY_ON_DRIVER_LCD, 2, 0, content);
		return send(msg);
	}

	/**
	 * 【0xA2】 报站文件切换 报站文件切换指令消息下发
	 */
	@Override
	public Result sendChangeLineMessage(String devCode, String newLineId, String direction) {
		Rm0xA2PlatformMessage msg = createChangeLineMessage(Integer.parseInt(devCode), Integer.parseInt(newLineId));
		return send(msg);
	}

	/**
	 * 【0xA6】【下发运营状态】【切换车辆运营状态】
	 * 
	 * @param devCode
	 *            设备编号
	 * @param typeValue
	 *            运营状态类型
	 * @param operateStatus	运营状态  0：非营运;1：运营
	 */
	@Override
	public Result sendChangeOperateMessage(String devCode, String typeValue, int operateStatus, String content) {
		Rm0xA6PlatformMessage msg = createChangeOperateMessage(Long.parseLong(devCode), typeValue,
				operateStatus, content);
		return send(msg);
	}

	@Override
	public Result sendUploadReportStationFileMessage(String devCode, Object data) {

		JSONObject obj = (JSONObject)data;
		Result resultMap = ResultUtil.getErrorResult("升级任务不存在 taskId:" + data);
		
		String updateModel = obj.getString("deviceModel");
		

		if ("FTP".equals(updateModel) || "F3R-FTP".equals(updateModel)) {

			Rm0xBCPlatformMessage msg = createUpdateBusFileByFTPMessage(devCode, obj.getString("sessionId"),
					obj.getIntValue("bftUdtype"), obj.getString("bufFtpUrl"), obj.getString("bufFilename"),
					PMSUtils.isNull(obj.getString("bufCrc")));
			resultMap = send(msg);
		} else if ("F3R".equals(updateModel)) {

			Rm0xC1PlatformMessage msg = createUpdateBusFileMessage(devCode, obj.getString("sessionId"), obj.getIntValue("bftUdtype"),
					obj.getString("bufFilename"), obj.getString("bufVersion"), obj.getIntValue("bufFilesize"),
					obj.getLongValue("bfdOffset"), obj.getString("bufCrc"));

			resultMap = send(msg);
		} else {
			
		}
		
		return resultMap;
	}



}
